package com.googlecode.microanalyst.palette.figure;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.swt.graphics.Color;

import com.googlecode.microanalyst.designer.figures.ConnectedFigure;
import com.googlecode.microanalyst.designer.figures.FixedConnectionAnchor;
import com.googlecode.microanalyst.palette.i18n.PaletteMessages;
import com.googlecode.microanalyst.palette.model.Smooth;

public class SmoothFigure extends ConnectedFigure {

	/** The inner TextFlow * */
	private TextFlow textFlow;

	private static final Dimension SIZE = new Dimension(16, 20);

	final static Color COLOR_FRAME = new Color(
			ColorConstants.tooltipBackground.getDevice(), 64, 64, 64);

	final static Color COLOR_FILL = new Color(
			ColorConstants.tooltipBackground.getDevice(), 204, 255, 255);
	
	public SmoothFigure() {
		super();

		//connection anchors
		int h2 = SIZE.height / 2;
		FixedConnectionAnchor inputAnchor, outputAnchor;
		inputAnchor = new FixedConnectionAnchor(this, FixedConnectionAnchor.LEFT, 0, h2);
		outputAnchor = new FixedConnectionAnchor(this, FixedConnectionAnchor.RIGHT, 0, h2);
		inputConnectionAnchors.add(inputAnchor);
		outputConnectionAnchors.add(outputAnchor);
		connectionAnchors.put(Smooth.INPUT, inputAnchor);
		connectionAnchors.put(Smooth.OUTPUT, outputAnchor);

		//text
		setBackgroundColor(ColorConstants.tooltipBackground);
		setForegroundColor(ColorConstants.tooltipForeground);

		FlowLayout centerLayout = new FlowLayout();
		centerLayout.setMajorAlignment(FlowLayout.ALIGN_CENTER);
		FlowPage flowPage = new FlowPage();
		flowPage.getInsets().top = 2;
		flowPage.getInsets().bottom = 3;
		flowPage.getInsets().left = 3;
		flowPage.getInsets().right = 3;

		textFlow = new TextFlow(PaletteMessages.Palette_Tool_Smooth_symbol);

		textFlow.setLayoutManager(new ParagraphTextLayout(textFlow,
				ParagraphTextLayout.WORD_WRAP_SOFT));

		flowPage.add(textFlow);
		setLayoutManager(centerLayout);
		add(flowPage);
		
	}

	@Override
	protected void paintFigure(Graphics graphics) {
		Rectangle rect = getBounds().getCopy();

		graphics.translate(getLocation());

		//main rect
		graphics.setBackgroundColor(COLOR_FILL);
		graphics.fillRectangle(0, 0, rect.width, rect.height);
		graphics.setBackgroundColor(COLOR_FRAME);
		graphics.drawRectangle(0, 0, rect.width - 1, rect.height - 1);
		
		//connection end points
		int h2 = rect.height / 2 - 1;
		graphics.fillRectangle(0, h2, 3, 3);
		graphics.fillRectangle(rect.width - 3, h2, 3, 3);

		graphics.translate(getLocation().getNegated());
	}
	
	public Dimension getPreferredSize(int wHint, int hHint) {
		return SIZE;
	}

}
